﻿

using QFramework;
using System.Collections.Generic;
using UnityEngine;

namespace ProjectChildhood
{
    public interface IPowerupSystem : QFramework.IToolBarSystem
    {
        public List<IPowerup> PowerUps { get; }
        void LoadData();
        void ResetData();
        void SaveData();
    }

    public class PowerupSystem : AbstractSystem, IPowerupSystem
    {
        public List<IPowerup> PowerUps { get; } = new List<IPowerup>();

        protected override void OnInit()
        {
            PowerUps.Add(new Powerup()
                .WithTitle("花洒范围")
                .WithPrice(30)
                .WithKey("waterscan_range_powerup")
                .WithDescribtion("花洒的作用范围+1")
                .Condition(_ =>Global.WaterScanUpgradeUnlock)
                .OnUnlock(up =>
                {
                    Global.WaterScanUpgradeUnlock = true;
                }));

            PowerUps.Add(new Powerup()
                .WithTitle("铁锹范围")
                .WithPrice(20)
                .WithKey("shovel_range_powerup")
                .WithDescribtion("铁锹的作用范围+1")
                .Condition (_ =>Global.ShovelUpgradeUnlock)
                .OnUnlock(up =>
                {
                    Global.ShovelUpgradeUnlock = true;
                }));

            PowerUps.Add(new Powerup()
               .WithTitle("手范围")
               .WithPrice(15)
               .WithKey("hand_range_powerup")
               .WithDescribtion("手的作用范围+1")
               .Condition(_ => Global.HandUpgradeUnlock)
               .OnUnlock(up =>
               {
                   Global.HandUpgradeUnlock = true;
               }));
        }

        public void LoadData()
        {
            Global.HandUpgradeUnlock = PlayerPrefs.GetInt(nameof(Global.HandUpgradeUnlock), 0) == 1;
            Global.WaterScanUpgradeUnlock = PlayerPrefs.GetInt(nameof(Global.WaterScanUpgradeUnlock), 0) == 1;
            Global.ShovelUpgradeUnlock = PlayerPrefs.GetInt(nameof(Global.ShovelUpgradeUnlock), 0) == 1;

            foreach (Powerup powerup in PowerUps)
            {
                PlayerPrefs.GetInt("power_up_" + powerup.Key + "_unlocked", 0);
            }
        }

        public void ResetData()
        {
            Global.HandUpgradeUnlock = false;
            Global.WaterScanUpgradeUnlock = false;
            Global.ShovelUpgradeUnlock = false;

            foreach (Powerup powerup in PowerUps)
            {
                powerup.Unlocked = false;
            }
            SaveData();
        }

        public void SaveData()
        {
            PlayerPrefs.SetInt(nameof(Global.HandUpgradeUnlock), Global.HandUpgradeUnlock ? 1 : 0);
            PlayerPrefs.SetInt(nameof(Global.WaterScanUpgradeUnlock), Global.WaterScanUpgradeUnlock ? 1 : 0);
            PlayerPrefs.SetInt(nameof(Global.ShovelUpgradeUnlock), Global.ShovelUpgradeUnlock ? 1 : 0);

            foreach (Powerup powerup in PowerUps)
            {
                PlayerPrefs.SetInt("power_up_" + powerup.Key + "_unlocked", powerup.Unlocked ? 1 : 0);
            }
        } 
    }
}
